<script setup lang="ts">
import { Delete, Edit, InfoFilled } from "@element-plus/icons-vue";
import { onMounted } from "vue";
import Dialog from "@/view/Resource/Tutorial/dialog.vue";
import useTutorial from "@/view/Resource/Tutorial/utils/hook.ts";

const {
  dialog,
  bodyStore,
  tutorialList,
  searchBody,
  addTutorial,
  restList,
  onSearchBody,
  EditTutorial,
  deleteTutorial,
  getTutorial
} = useTutorial()

onMounted( () => {
  getTutorial()
})
</script>

<template>
  <el-card class="box-card">
    <div class="card-tutorial-title">
      <div class="search-tutorial align_center row">
        <el-select v-model="searchBody" clearable placeholder="Select">
          <el-option
            v-for="item in bodyStore.bodyList"
            :key="item._id"
            :label="item.nickname"
            :value="item._id"
          />
        </el-select>
        <el-button type="primary" @click="onSearchBody" style="margin:0 10px">搜索</el-button>
        <el-button @click="restList">重置</el-button>
      </div>
      <el-button type="primary" @click="addTutorial">添加教程</el-button>
    </div>
    <div class="card-tutorial-table">
      <el-table
        border
        class="el-table-box"
        ref="multipleTableRef"
        :data="tutorialList"
        style="width: 100%"
      >
        <el-table-column label="#" width="60" align="center" type="index"/>
        <el-table-column align="center" label="性别" width="60">
          <template #default="{row}">
            {{ row.gender === 0 ? '男' : '女' }}
          </template>
        </el-table-column>
        <el-table-column prop="name" align="center" label="名称"/>
        <el-table-column align="center" label="部位">
          <template #default="{row}">
            {{ row.bodyId.nickname }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="描述" width="200">
          <template #default="{row}">
            <div class="tutorial-desc">
              {{ row.desc[0] }}
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="控制" width="220">
          <template #default="{row}">
            <div class="operator-btn">
              <el-button type="primary" size="default" @click="EditTutorial(row)" :icon="Edit as any" circle/>
              <el-popconfirm
                width="200"
                @confirm="deleteTutorial(row._id)"
                confirm-button-text="确定"
                cancel-button-text="取消"
                :icon="InfoFilled as any"
                icon-color="#f36b6c"
                title="您确定删除此部位吗?"
              >
                <template #reference>
                  <el-button type="danger" size="default" :icon="Delete as any" circle/>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </el-card>
  <Dialog ref="dialog" @get="getTutorial"/>
</template>

<style scoped lang="scss">
.box-card {
  padding: 0 50px;

  .card-tutorial-title {
    height: 35px;
    display: flex;
    justify-content: space-between;
  }

  .card-tutorial-table {
    margin-top: 20px;

    .el-table-box {
      height: calc(90vh - 140px);
    }

    .tutorial-desc {
      display: -webkit-box; /*弹性伸缩盒子*/
      -webkit-box-orient: vertical; /*垂直排列*/
      line-height: 20px;
      -webkit-line-clamp: 1;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}
</style>
